package org.example.controller;


import org.example.GetConnection;
import org.example.bean.BorrowDto;
import org.example.info.Book;
import org.example.info.Result;
import org.springframework.web.bind.annotation.*;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
//书籍的添加，删除，修改
@RestController
@CrossOrigin(origins = "*")
public class BookController {
    private Connection connection = new GetConnection().GetConnection();
    private PreparedStatement statement;
    private ResultSet resultSet;

    @GetMapping("/book")
    public Result<?> getBook() throws SQLException {
        List<Book> bookList = new ArrayList<>();

        statement =  connection.prepareStatement("select * from book");
        resultSet=statement.executeQuery();
        while (resultSet.next()){
            bookList.add(new Book(resultSet.getInt("id"),resultSet.getString("name"),resultSet.getString("publish"),resultSet.getInt("store"),resultSet.getInt("labelID"),resultSet.getInt("authorID")));
        }
        return Result.success(bookList);
    }

    @PostMapping("/book")
    public Result<?> addBook(@RequestBody Book book) throws SQLException {
        System.out.println(book);
        statement = connection.prepareStatement("insert into book (name,authorID,LabelID,store,publish) values (?,?,?,?,?)");
        statement.setString(1, book.getName());
        statement.setInt(2, book.getAuthorID());
        statement.setInt(3,book.getLabelID());
        statement.setInt(4,book.getStore());
        statement.setString(5,book.getPublish());
        statement.executeUpdate();
        return Result.success();
    }

    @PutMapping("/book/{id}")
    public Result<?> updateBook(@PathVariable("id") Integer bookid, @RequestBody Book book) throws SQLException {
        statement = connection.prepareStatement("update book set name=?,authorID=?,LabelID=?,store=?,publish=? where id = ?");
        statement.setString(1, book.getName());
        statement.setInt(2, book.getAuthorID());
        statement.setInt(3, book.getLabelID());
        statement.setInt(4,book.getStore());
        statement.setString(5,book.getPublish());
        statement.setInt(6,bookid);
        statement.executeUpdate();
        return Result.success();
    }

    @DeleteMapping("/book/{id}")
    public Result<?> deleteBook(@PathVariable("id") Integer id) throws SQLException {
        statement = connection.prepareStatement("delete from book where id = ?");
        statement.setInt(1, id);
        statement.executeUpdate();
        return Result.success();
    }

    @PostMapping("/book/borrow")
    public Result<?> borrowBook(@RequestBody BorrowDto borrowDto) throws SQLException {
        // 库存 -1
        statement = connection.prepareStatement("update book set store = store-1 where id = ?");
        statement.setInt(1,borrowDto.getBookID());
        statement.executeUpdate();

        // 新增记录
        statement = connection.prepareStatement("insert into record(bookID,userID,type,time) values (?,?,1,?)");
        statement.setInt(1,borrowDto.getBookID());
        statement.setInt(2,borrowDto.getUserID());
        statement.setDate(3, new Date(new java.util.Date().getTime()));

        statement.executeUpdate();

        return Result.success();
    }

    @PostMapping("/book/return")
    public Result<?> returnBook(@RequestBody BorrowDto borrowDto) throws SQLException {
        // 库存 +1
        statement = connection.prepareStatement("update book set store = store+1 where id = ?");
        statement.setInt(1,borrowDto.getBookID());
        statement.executeUpdate();

        // 新增记录 type = 2
        statement = connection.prepareStatement("insert into record(bookID,userID,type,time) values (?,?,2,?)");
        statement.setInt(1,borrowDto.getBookID());
        statement.setInt(2,borrowDto.getUserID());
        statement.setDate(3, new Date(new java.util.Date().getTime()));

        statement.executeUpdate();

        return Result.success();
    }

}
